package com.atguigu.yygh.hosp.testmongo;


import com.atguigu.yygh.common.result.R;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/mongo2")
public class MongoTest2 {

    //注入
    @Autowired
    private UserRepository userRepository;

    //添加
    @ApiOperation("添加")
    @GetMapping("create")
    public void createUser() {
        User user = new User();
        user.setAge(20);
        user.setName("张三");
        user.setEmail("3332200@qq.com");
        userRepository.save(user);
    }

    //查询所有
    @ApiOperation("查询所有")
    @GetMapping("findAll")
    public R findUser() {
        List<User> list = userRepository.findAll();
        return R.ok().data("list",list);
    }

    //查询id
    @ApiOperation("id查询")
    @GetMapping("findUserId/{id}")
    public R findId(@PathVariable String id) {
        User user = userRepository.findById(id).get();
        return R.ok().data("user",user);
    }

    //查询条件
    @ApiOperation("条件查询")
    @GetMapping("findUserQuery")
    public R findUserQuery() {
        User user = new User();
        user.setName("张三");
        Example<User> example = Example.of(user);
        List<User> list = userRepository.findAll(example);
        return R.ok().data("list",list);
    }

    //模糊查询
    @ApiOperation("模糊查询")
    @GetMapping("findLike")
    public R findUsersLikeName() {
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        User user = new User();
        user.setName("三");
        Example<User> userExample = Example.of(user, matcher);
        List<User> userList = userRepository.findAll(userExample);
        return R.ok().data("list",userList);
    }

    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("findPage")
    public R findUsersPage() {
        //1 设置排序
        Sort sort = Sort.by(Sort.Direction.DESC,"age");

        //2 设置分页参数
        //第一个参数当前页：0代表第一页
        //第二个参数每页显示记录数
        //第三个参数排序对象
       Pageable pageable = PageRequest.of(0,2,sort);

        //3 设置条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        User user = new User();
        user.setName("三");
        Example<User> userExample = Example.of(user, matcher);

        //4 调用方法得到分页查询结果
        Page<User> pages = userRepository.findAll(userExample, pageable);
        List<User> list = pages.getContent();
        return R.ok().data("list",list);
    }

    //修改
    @ApiOperation("修改")
    @GetMapping("update")
    public void updateUser() {
        User user = userRepository.findById("61b343f729918e6ac80155e8").get();
        user.setName("张三丰");
        userRepository.save(user);
    }

    //删除
    @ApiOperation("删除")
    @GetMapping("delete")
    public void delete() {
        userRepository.deleteById("61b343f729918e6ac80155e8");
    }

    // where name=? and age=?
    @GetMapping("test1")
    public R test1() {
        List<User> list = userRepository.findUserByNameAndAge("张三",20);
        return R.ok().data("list",list);
    }

    // where name like ?
    @GetMapping("test2")
    public R test2() {
        List<User> list = userRepository.findUserByNameLike("张");
        return R.ok().data("list",list);
    }
}
